<html>
<head>
    <title> Test Database </title>
</head>
<body>

<?php

		require_once( 'core/core.php' );
    //require_once( 'db.php' );

    // create database object and connect to database
    //db() = new Database();

    /*
     * ici, on cree la table bonjour - si elle exist, on la drop et recree
     */
    $createit = true;
    while( $createit )
    {
        print( 'Creating table "bonjour"<br>'."\n" );
        if( !db()->execute( 'CREATE TABLE bonjour (a INT, b INT, txt TEXT)' ) )
        {
            print( 'Unable to create table ('.db()->lastError().')<br>'."\n" );

            print( 'droping table bonjour <br>'."\n" );
            if( !db()->execute( 'DROP TABLE bonjour' ) )
            {
                print( 'Serious error ('.db()->lastError().') - cannot do anything! <br>'."\n" );
                return;
            }
        }
        else
        {
            $createit = false;
        }
    }
    print( '<br>'."\n" );

    /*
     * ici, on insert des data
     */
    print( 'Inserting into "bonjour"<br>'."\n" );
    $txt = 'Here is a super text to insert';
    $txt = db()->escapeStr( $txt ); // we escape it in case there is weird characters in it
    if( !db()->execute( 'INSERT INTO bonjour VALUES (1, 2, '."'".$txt."'".')' ) )
    {
        print( 'Unable to insert values ('.db()->lastError().')<br>'."\n" );
        return;
    }
    print( '<br>'."\n" );

    /*
     * ici, on update
     */
    print( 'Updating "bonjour"<br>'."\n" );
    $txt = 'Update me - update me!';
    $txt = db()->escapeStr( $txt );
    if( !db()->execute( 'UPDATE bonjour SET a=12, txt='."'".$txt."'" ) )
    {
        print( 'Unable to update value<br>'."\n" );
        return;
    }
    print( '<br>'."\n" );

    /*
     * ici, on insert 10 records d'un coup! youhou!
     */
    print( 'Insert 10 records<br>'."\n" );
    for( $i = 0; $i < 10; $i++ )
    {
        if( !db()->execute( 'INSERT INTO bonjour (a) VALUES ('.$i.')' ) )
        {
            print( 'Unable to insert record '.$i.'<br>'."\n" );
            return;
        }
    }
    print( '<br>'."\n" );

    /*
     * ici, on select tout!
     */
    print( 'Selecting everything<br>'."\n" );
    $results = db()->select( 'SELECT * FROM bonjour' );
    print( 'There is '.$results->rowCount().' rows with '.$results->columnCount().' fields per row<br>'."\n" );

    $i = 0;
    while( ($row = $results->getNext()) )
    {
        print( 'row '.$i++.' a:'.$row['a'].' b:'.$row['b'].' txt:'.$row['txt'].' <br>'."\n" );
    }

    print( '<br>'."\n" );

    /*
     * ici, on select juste 1 enregistrement - pas besoin de getNext()
     */
    print( 'Select one specific row<br>'."\n" );
    $results = db()->select( 'SELECT * FROM bonjour WHERE a=12 LIMIT 1' );

    $a = $results->getVal( 'a' );
    $b = $results->getVal( 'b' );
    $txt = $results->getVal( 'txt' );

    print( "one result: a:$a b:$b txt:$txt <br>\n" );
    print( '<br>'."\n" );

    /*
     * bah voila :)
     */
    print( "everything all right!<br>\n" );

    // LA DECONNECTION EST AUTOMATIQUE
    // (appel de disconnect() dans le destructor de l'object Database
?>

</body>
</html>
